<script>
import { checkSession } from './utils/index';
import { mapActions } from 'vuex';
export default {
  mpType: 'app',
  onLaunch() {
  },
  onShow() {
    this.login();
  },
  onHide() {

  },
  methods: {
    ...mapActions(['getSession']),
    async login() {
      const isAuth = await this.isAuth();
      const isExpired = await checkSession();
      const token = mpvue.getStorageSync('token');
      if (!isAuth) {
        // 未授权
        mpvue.removeStorageSync('token');
        if (this.$router.currentRoute.path !== '/pages/auth/index') {
          this.$router.replace({
            path: '/pages/auth/index'
          });
        }
      } else if (!token || isExpired) {
        mpvue.getUserInfo({
          success: (res) => {
            const nick = res.userInfo.nickName;
            mpvue.login({
              success: (res) => {
                const code = res.code;
                // 授权接口调用
                this.getSession({
                  code: code,
                  nick: nick,
                });
              },
              fail: (err) => {
                mpvue.showToast({
                  title: '授权失败，请重试',
                  icon: 'none'
                });
              }
            });
          },
          fail: (err) => {
            mpvue.showToast({
              title: '授权失败，请重试',
              icon: 'none'
            });
          }
        });
      } else {
        // 已授权跳转到首页
        console.log('已授权&&未过期');
        let pages = getCurrentPages();
        if (pages.length) {
          if (pages[pages.length - 1].route == 'pages/auth/index') {
            mpvue.reLaunch({
              url: '/pages/advise/index'
            });
          }
        } else {
          mpvue.reLaunch({
            url: '/pages/advise/index'
          });
        }
      }
    },
    isAuth() {
      return new Promise((resolve, reject) => {
        mpvue.getSetting({
          success: (res) => {
            if (res.authSetting['scope.userInfo']) {
              this.$store.commit('setAuth', true);
              mpvue.getUserInfo({
                success: (res) => {
                  this.$store.commit('setUserInfo', res.userInfo);
                }
              });
              resolve(true);
            } else {
              console.log('未授权');
              this.$store.commit('setAuth', false);
              resolve(false);
            }
          },
          fail: (err) => {
            this.$store.commit('setAuth', false);
            resolve(false);
          }
        });
      });
    }
  }
}
// "tabBar": {
  //   "color": "#999",
  //   "backgroundColor": "#fafafa",
  //   "selectedColor": "#333",
  //   "borderStyle": "white",

  //   "list": [
  //     {
  //       "text": "首页",
  //       "pagePath": "pages/index/index",
  //       "iconPath": "static/tabs/home.png",
  //       "selectedIconPath": "static/tabs/home-active.png"
  //     },
  //     {
  //       "text": "订单",
  //       "pagePath": "pages/logs/index",
  //       "iconPath": "static/tabs/orders.png",
  //       "selectedIconPath": "static/tabs/orders-active.png"
  //     }
  //   ],
  //   "position": "bottom"
  // }
</script>
